/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.oop.example.data;

import java.io.File;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author -
 */
public class HibernateUtil {

    public static final String url = "hibernate.connection.url";
    private static final SessionFactory sessionFactory;
    private static AnnotationConfiguration cfg;

    static {
        //jdbc:h2:c:\Users\-\Google Диск\Abby\Parser\abbyy.h2.db
        File f = new File("abbyy.h2.db");
        File hcfg = new File("hibernate.cfg.xml");
        try {
            cfg = new AnnotationConfiguration();
            cfg.setProperty(url, "jdbc:h2:" + f.getAbsolutePath());
            cfg.configure(hcfg);
            sessionFactory = cfg.buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static synchronized SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static String getProperty(String name) {
        return cfg.getProperty(name);

    }

    public static void main(String[] args) {
        Session openSession = HibernateUtil.getSessionFactory().openSession();
        Transaction beginTransaction = openSession.beginTransaction();
        Query createQuery = openSession.createQuery("update EntryPOJO a set a.Status = 3 where a.ID <= 2272 and a.ID >= 1432");
        createQuery.executeUpdate();        
        beginTransaction.commit();
        openSession.close();
    }
}
